﻿#include "stdafx.h"
#include "frmFileBrowser.h"
using namespace rjksClient;

String^ HumanizeSize(long size)
{
	if(size>1000000000L)//G
		return Math::Round((double)size/Math::Pow(1024,3),2).ToString()+"G";
	if(size>1000000L)//M
		return Math::Round((double)size/Math::Pow(1024,2),2).ToString()+"M";
	if(size>1024)//K
		return Math::Round((double)size/(double)1024,2).ToString()+"K";
	return size.ToString()+"B";
}
SizeF MeasureSize(String^ str,Font^ font,Graphics^ g)
{
	g->PageUnit = GraphicsUnit::Pixel;
	g->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::HighQuality;
	StringFormat^ sf = gcnew StringFormat();
	sf->FormatFlags = StringFormatFlags::MeasureTrailingSpaces;
	return g->MeasureString(str,font, 500, sf);
}

System::Void frmFileBrowser::lstFiles_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e) {
	if (this->lstFiles->Items->Count==0)
		return
	e->DrawBackground();
	Graphics^ g = e->Graphics;
	_FileInfo item=(_FileInfo) this->lstFiles->Items[e->Index];
	Brush^ brush;
	bool selected=(e->State & DrawItemState::Selected) == DrawItemState::Selected;
	if (selected)
		brush=gcnew SolidBrush(Color::FromArgb(0xff8e44ad));
	else
		brush = (e->Index) %2 ? Brushes::WhiteSmoke :gcnew SolidBrush(e->BackColor);
	g->FillRectangle(brush, e->Bounds);
	//filename
	e->Graphics->DrawString(Path::GetFileNameWithoutExtension(item.filename), 
				gcnew System::Drawing::Font(this->Font->Name,12.0F,FontStyle::Bold),
				selected ? Brushes::White : Brushes::SlateGray, 0,e->Bounds.Top, StringFormat::GenericDefault); 
	//ext
	String^ ext;
	if(Path::GetExtension(item.filename)->Length>1)
		ext="["+Path::GetExtension(item.filename)->Substring(1)+"]";
	else
		ext="";
	System::Drawing::Font^ extfont=gcnew System::Drawing::Font(this->Font->Name,10.0F,FontStyle::Bold);
	e->Graphics->DrawString(ext,extfont,
				selected ? Brushes::White : gcnew SolidBrush(Color::FromArgb(0xffc0392b)),
				e->Bounds.Width-MeasureSize(ext,extfont,g).Width,e->Bounds.Top, StringFormat::GenericDefault); 
	//time
	e->Graphics->DrawString(item.time.ToString("yy-MM-dd hh:mm"), 
		gcnew System::Drawing::Font(this->Font->Name,7.0F,FontStyle::Regular),
				selected ? Brushes::White : Brushes::Gray, 0,e->Bounds.Top+35, StringFormat::GenericDefault); 
	//size
	String^ sz=HumanizeSize(item.size);
	String^ sz1=sz->Substring(0,sz->Length-1);
	String^ sz2=sz->Substring(sz->Length-1);
	System::Drawing::Font^ szfont=gcnew System::Drawing::Font(this->Font->Name,8.5F,FontStyle::Regular);
	e->Graphics->DrawString(sz1, szfont,
				selected ? Brushes::White : gcnew SolidBrush(Color::FromArgb(0xfff39c12)),
				e->Bounds.Width-MeasureSize(sz,szfont,g).Width,
				e->Bounds.Top+30, StringFormat::GenericDefault); 
	szfont=gcnew System::Drawing::Font(this->Font->Name,8.5F,FontStyle::Bold);
	e->Graphics->DrawString(sz2, szfont,
				selected ? Brushes::White : gcnew SolidBrush(Color::FromArgb(0xfff39c12)),
				e->Bounds.Width-MeasureSize(sz2,szfont,g).Width,
				e->Bounds.Top+31, StringFormat::GenericDefault); 
	e->DrawFocusRectangle();
}
void frmFileBrowser::RefreshControls()
{
	JObject^ jo=AsyncHttpUtils::GetResponse("file/list");
	//lblName->Text=jo["uname"]->ToString();
	int cnt=0;
	_FileInfo f;
	lstFiles->Items->Clear();
	if(jo["msg"]->ToString()=="nothing"){
		lblFileCnt->Text="暂无文件";
		return;
	}
	for each (JToken^ jt in Utils::SafeWrapper(jo))
	{
		f.filename=Utils::Urldecode(jt["filename"]->ToString());
		f.size=int::Parse(jt["size"]->ToString());
		f.hash=jt["hash"]->ToString();
		f.fid=int::Parse(jt["fid"]->ToString());
		f.time=DateTime::Now;
		lstFiles->Items->Add(f);
		cnt++;
	}
	lblFileCnt->Text="共有"+cnt.ToString()+"个文件";
}
System::Void frmFileBrowser::btnUpload_Click(System::Object^  sender, System::EventArgs^  e) {
			 openFileDialog1->Title="选择上传文件";
			 
			 if(openFileDialog1->ShowDialog()==::DialogResult::OK){
				 this->has_shown=false;
				AsyncHttpUtils::GetFileUploadResponse("file/upload",openFileDialog1->FileName,
				gcnew Action<int>(this,&frmFileBrowser::SetProgressBar));
				RefreshControls();
			 }
}
System::Void frmFileBrowser::btnDownload_Click(System::Object^  sender, System::EventArgs^  e) {
			 if(lstFiles->SelectedIndex==-1){
				 (gcnew frmMsgBox)->ShowCritical("没有选择文件！");
					 return;
			 }
			 saveFileDialog1->Title="下载\""+((_FileInfo)(this->lstFiles->SelectedItem)).filename+"\"到:";
			 saveFileDialog1->FileName=((_FileInfo)(this->lstFiles->SelectedItem)).filename;
			 if(saveFileDialog1->ShowDialog()==::DialogResult::OK)
			 {
				 this->has_shown=false;
				 Stream^ memstream=AsyncHttpUtils::GetSimpleGETResponse("file/download?fid="+
						Utils::Urlencode(((_FileInfo)(this->lstFiles->SelectedItem)).fid.ToString()),
					 gcnew Action<int>(this,&frmFileBrowser::SetProgressBar));
				 try
			   {
				int osize=1;
				array<Byte>^buf = gcnew array<Byte>(2048000);//2M
				Stream^ out =System::IO::File::Create( saveFileDialog1->FileName );
				while(osize>0)
				{
					osize=memstream->Read(buf,0,buf->Length);
					out->Write(buf,0,osize);
					Application::DoEvents();
				}
				memstream->Close();
				out->Close();
				} catch (IOException^ ex)
			   {
				(gcnew frmMsgBox)->ShowInfo(ex->Message);
			   }
			 }
		 }
void frmFileBrowser::SetProgressBar(int progress)
{
	if (this->has_shown)
		return;
	if(progress>=100)
	{
		if (this->lstFiles->SelectedIndex==-1)
		{
			(gcnew frmMsgBox)->ShowInfo("上传完成！");
		}else
		{
			this->setTitleText("下载："+((_FileInfo)(this->lstFiles->SelectedItem)).filename+" 完成100%");
			(gcnew frmMsgBox)->ShowInfo(((_FileInfo)(this->lstFiles->SelectedItem)).filename+"\n下载完成！");
		}
		this->setTitleText("文件管理器");
		this->progressBar1->Value=0;
		this->has_shown=true;
	}else
	{
	this->progressBar1->Value=progress;
	if (this->lstFiles->SelectedIndex!=-1)
		this->setTitleText("下载："+((_FileInfo)(this->lstFiles->SelectedItem)).filename+" 完成"+progress+"%");
	}

}